package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.Word;
import com.qf.enums.OAExceptionEnum;
import com.qf.exception.OAException;
import com.qf.service.WordgyhService;
import com.qf.utils.NumberUtil;
import com.qf.utils.R;
import com.qf.utils.WendangUtils;
import com.qf.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author 程云
 * @Date 2019/12/12 14:13
 * 人生在世 代码二字
 */
@RequestMapping("/resource")
@Controller
@Slf4j
public class DocRecycleController {

    @Autowired
    private WordgyhService wordgyhService;

    //访问文档回收站
    @GetMapping("/wendang/demo2/list")
    public String doclist(Model model,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer size){
        //查询全部文档列表
        PageInfo<Word> list = wordgyhService.findAllRecycle(page,size);
        model.addAttribute("listDoc",list);
        return "resource/wendang/demo2/list";
    }

    //删除单个(真删除)
    @PostMapping("/wendang/del")
    @ResponseBody
    public ResultVO wendangDel(Integer id){
        System.out.println(id);
        Integer count = wordgyhService.del(id);
        if (count != 1){
            log.info("【删除回收站】 删除失败 count = {}",count);
            return R.error(OAExceptionEnum.TRIP_DELETE_ERROR.getCode(), OAExceptionEnum.TRIP_DELETE_ERROR.getMsg());
        }
        return R.ok();
    }

    //还原
    @PostMapping("/wendang/huanyuan")
    @ResponseBody
    public ResultVO huanyuan(Integer id){
        Integer count = wordgyhService.huan(id);
        if(count != 1){
            log.info("【还原信息】 还原失败 count = {},id = {}", count,id);
            return R.error(OAExceptionEnum.HUAN_YUAN_ERROR.getCode(),OAExceptionEnum.HUAN_YUAN_ERROR.getMsg());
        }
        return R.ok();
    }

    //多选删除+导出
    @PostMapping("/wendang/deleteAll")
    public void deletes(int[] wordId, HttpServletResponse response, String method) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        System.out.println(wordId);
        if (wordId == null){
            response.getWriter().print("<script>alert('您还没有选择数据');location.href='/resource/wendang/demo2/list'</script>");
            log.info("【多选】 参数错诶 wordId = {}",wordId);
            return;
            //throw new OAException(OAExceptionEnum.DUOUAN_ERROR.getMsg(),OAExceptionEnum.DUOUAN_ERROR.getCode());
        }


            if(method.equals("delete")){
                int i = wordgyhService.deletes(wordId);
                if(i>0){
                    response.getWriter().print("<script>alert('删除成功');location.href='/resource/wendang/demo2/list'</script>");
                }else{
                    response.getWriter().print("<script>alert('删除失败');location.href='/resource/wendang/demo2/list'</script>");
                }
            }else if(method.equals("daochu")){
                try {

                    //导出数据
                    //1.查询被选中的数据信息
                    List<Word> employeeList = wordgyhService.findbyids(wordId);
                    //2.导出数据
                    WendangUtils.createFirst("文档信息");
                    //设置导出格式
                    response.setContentType("application/x-download");
                    //生成随机名字
                    String number = NumberUtil.getNumber(6);
                    String excleName = "excle_"+number+ ".xls";
                    response.addHeader("Content-Disposition", "attachment;filename=" + excleName);
                    WendangUtils.createOthers(employeeList);
                    /*SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");*/
                    /*new FileOutputStream("F://"+(format.format(new Date()))+".xlsx")*/
                    WendangUtils.export(response.getOutputStream());

                   /* response.getWriter().print("<script>alert('导出成功');location.href='/resource/wendang/demo2/list'</script>");*/
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    }

}
